package com.example.foobar.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.foobar.business.service.DidiOrderService;
import com.example.foobar.business.service.DidiService;
import com.example.foobar.dto.DidiURL;
import com.example.foobar.dto.DidiURLRequestDTO;
import com.example.foobar.entity.DaConfig;
import com.example.foobar.entity.DidiOrders;
import com.example.foobar.param.DidiCallBackPageParam;
import com.example.foobar.param.DidiDateParam;
import com.example.foobar.param.DidiOrdersPageParam;
import com.example.foobar.vo.DidiData;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 分析系统配置文件 控制器
 *
 * @author 郑英奔
 * @since 2021-05-31
 */
@Slf4j
@RestController
@RequestMapping("/didi")
@Module("didiController")
@Api(value = "滴滴相关API", tags = {"滴滴"})
public class DidiController extends BaseController {

    @Autowired
    private DidiService didiService;

    @Autowired
    private DidiOrderService didiOrderService;


    /**
     * 获取分析系统配置文件详情
     */
    @GetMapping("/getURL")
    @OperationLog(name = "获取滴滴推广链接", type = OperationLogType.INFO)
    @ApiOperation(value = "获取滴滴推广链接", response = ApiResult.class)
    public ApiResult<DidiURL> getDaConfig(String activityId, String linkType, String promotionId, String sourceId) throws Exception {
        DidiURLRequestDTO didiURLRequestDTO = new DidiURLRequestDTO();
        didiURLRequestDTO.setActivityId(activityId);
        didiURLRequestDTO.setLinkType(linkType);
        didiURLRequestDTO.setPromotionId(promotionId);
        didiURLRequestDTO.setSourceId(sourceId);
        DidiURL didiURL = didiService.getURL(didiURLRequestDTO);
        return ApiResult.ok(didiURL);
    }


    /**
     * 获取分析系统配置文件详情
     */
    @OperationLog(name = "获取滴滴回调链接")
    @ApiOperation(value = "获取滴滴回调链接")
    @ResponseBody
    @RequestMapping(value = "/callback", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ApiResult<Boolean> callback(@RequestBody JSONObject jsonParam) throws Exception {
        log.info("------------------------------callback---------------------------");
        // 直接将json信息打印出来
        log.info("订单号： " + jsonParam.get("order_id")+"，支付时间：" + jsonParam.get("pay_time"));
        DidiOrders didiOrders = JSON.toJavaObject(jsonParam, DidiOrders.class);
        boolean flag = didiOrderService.saveOrUpdateByOrderId(didiOrders);
        // 将获取的json数据封装一层，然后在给返回
        log.info("-------------------------------------------------------------------");
        return ApiResult.result(ApiCode.DIDI_SUCCESS).setSuccess(flag);
    }

    public JSONObject getJSONParam(HttpServletRequest request){
        JSONObject jsonParam = null;
        try {
            // 获取输入流
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));

            // 写入数据到Stringbuilder
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = streamReader.readLine()) != null) {
                sb.append(line);
            }
            jsonParam = JSONObject.parseObject(sb.toString());
            // 直接将json信息打印出来
//            System.out.println(jsonParam.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonParam;
    }

    /**
     * 滴滴订单分页列表
     */
    @PostMapping("/getDidiDateList")
    @OperationLog(name = "获取滴滴每天数据", type = OperationLogType.PAGE)
    @ApiOperation(value = "获取滴滴每天数据", response = DidiOrders.class)
    public ApiResult<Paging<DidiData>> getDidiDateList(@Validated @RequestBody DidiDateParam didiDateParam) throws Exception {
        Paging<DidiData> paging = didiOrderService.getDidiDateList(didiDateParam);
        return ApiResult.ok(paging);
    }
}

